/*!
    \file  main.c
    \brief GPIO running led
*/

/*
    Copyright (C) 2017 GigaDevice

    2017-07-28, V1.0.0, demo for GD32F30x
*/

#include "gd32f30x.h"
#include "gd32f307c_eval.h"
#include "systick.h"

#include "includes.h"

#include "globle.h"
#include "Flash_Port.h"
#include "CommInterface.h"

#define INIT_TASK_PRIO          (OS_LOWEST_PRIO - 4u)
#define LED2_TASK_PRIO          (OS_LOWEST_PRIO - 7u)
#define LED3_TASK_PRIO          (OS_LOWEST_PRIO - 8u)

#define INIT_TASK_STK_SIZE      32
#define LED_TASK_STK_SIZE       128

OS_STK InitTaskStack[INIT_TASK_STK_SIZE];
OS_STK LED2TaskStack[LED_TASK_STK_SIZE];
OS_STK LED3TaskStack[LED_TASK_STK_SIZE];

void LED_Configuration(void);
void vTaskInit(void *pvParameters);
void vTaskLED2(void *pvParameters);
void vTaskLED3(void *pvParameters);

/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
int main(void)
{  
    /* configurate LED, NVIC and SysTick */
//    nvic_vector_table_set(0x8000000,0x4000);	
    nvic_vector_table_set(0x8000000,0x0000);	
    nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
    LED_Configuration();
  //  nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
    SysTick_Config(SystemCoreClock / OS_TICKS_PER_SEC);
    
    OSInit();
    /* init task */
    OSTaskCreate(vTaskInit,
                 (void *)0,
                 (OS_STK *)&InitTaskStack[INIT_TASK_STK_SIZE-1],
                 INIT_TASK_PRIO);
    
    /* start scheduler */
    OSStart();

    while(1){
    }
}

/*!
    \brief      Configure the GPIO ports
    \param[in]  none
    \param[out] none
    \retval     none
*/
void LED_Configuration(void)
{
    gd_eval_led_init(LED2);
    gd_eval_led_init(LED3);
    gd_eval_com_init(EVAL_COM1);
}

/*!
    \brief      initilization task, initilize the LED tasks
    \param[in]  pvParameters: passing parameter when the task is created
    \param[out] none
    \retval     none
*/
void vTaskInit(void *pvParameters)
{
    /* start toogle LED2 task every 500ms */
    globleInit ();
    CommInit(&gCommCfg);
    OSTaskCreate(vTaskLED2,
                 (void *)0,
                 (OS_STK *)&LED2TaskStack[LED_TASK_STK_SIZE-1],
                 LED2_TASK_PRIO);
    
//    /* start toogle LED3 task every 500ms */
//    OSTaskCreate(vTaskLED3,
//                (void *)0,
//                (OS_STK * )&LED3TaskStack[LED_TASK_STK_SIZE-1],
//                LED3_TASK_PRIO);
    
    while(1){
        OSTaskDel(OS_PRIO_SELF);
    }
}

/*!
    \brief      LED2 task, turn on and off the LED2
    \param[in]  pvParameters: passing parameter when the task is created
    \param[out] none
    \retval     none
*/
void vTaskLED2(void *pvParameters)
{
    while(1){
        gd_eval_led_toggle(LED2);
        OSTimeDly(500);
    }
}

/*!
    \brief      LED3 task, turn on and off the LED3
    \param[in]  pvParameters: passing parameter when the task is created
    \param[out] none
    \retval     none
*/
//uint8_t test_buffer[4096];
void vTaskLED3(void *pvParameters)
{
    static int flag = 0;
    uint32_t flash_id = 0,i;
    spi_flash_init();
    flash_id = spi_flash_read_id();
    printf("flash_id : %x \n",flash_id);

    while(1){
//      spi_flash_buffer_read(test_buffer,FLASH_ADDR_BLOCK_0,4096);  
//      test_buffer[4095] = 0; 
//      printf("%s\n",test_buffer);
        if(flag == 0){
            gd_eval_led_off(LED3);
            flag = 1;
        }else{
            gd_eval_led_on(LED3);
            flag = 0;
        }
        OSTimeDly(500);
    }
}
